﻿#include "MathUtil.h"
#include <iosfwd>


MathUtil::MathUtil(void)
{
}


MathUtil::~MathUtil(void)
{
}


float MathUtil::angle2Vetor( const CCPoint& a, const CCPoint& b )
{
	if ((a.x * b.y - a.y * b.x) >= 0)
		return acos(dotProduct(a, b) / (absVector(a) * absVector(b)));
	else
		return -acos(dotProduct(a, b) / (absVector(a) * absVector(b)));
}

float MathUtil::absVector( const CCPoint& a )
{
	return sqrt(a.x * a.x + a.y * a.y);
}

float MathUtil::dotProduct( const CCPoint& a, const CCPoint& b )
{
	return a.x * b.x + a.y * b.y;
}

cocos2d::CCPoint MathUtil::rotateVector( const CCPoint& a, float rad )
{
	float x = a.x * cos(rad) - a.y * sin(rad);
	float y = a.x * sin(rad) + a.y * cos(rad);
	return ccp(x, y);
}

int MathUtil::randonInt( const int& from, const int& to )
{
	return rand() % (to + 1 - from) + from;
}

float MathUtil::RadToAngleDegree( float rad )
{
	return rad / M_PI * 180;
}

float MathUtil::DegreeToRad( float deg )
{
	return deg * M_PI / 180;
}

std::string MathUtil::IntToString( int num )
{
	ostringstream ss;
	ss << num;
	return ss.str();
}

std::string MathUtil::FloatToString( float num, int precision)
{
	ostringstream ss;
	ss.setf(ios::fixed);
	ss.precision(precision);
	ss << num;
	return ss.str();
}
